package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.bean.Application;
import com.it.mapper.ApplicationMapper;
import com.it.page.PageQuery;
import com.it.service.ApplicationService;
import com.it.utils.AppUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("applicationService")
@RequiredArgsConstructor
public class ApplicationServiceImpl implements ApplicationService {

    private final ApplicationMapper applicationMapper;

    @Override
    public PageInfo<Application> page(PageQuery<Application> pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());
        Application condition = pageQuery.getQueryBody();
        condition.setDeleted(false);
        List<Application> ds = applicationMapper.select(condition);
        return new PageInfo<>(ds);
    }

    @Override
    public void delete(Long id) {
        Application application = applicationMapper.selectByPrimaryKey(id);
        application.setDeleted(true);
        AppUtils.forUpdateAudit(application);
        applicationMapper.updateByPrimaryKeySelective(application);
    }

    @Override
    public void save(Application application) {
        if (application.getId() == null) {
            AppUtils.forSaveAudit(application);
            applicationMapper.save(application);
        } else {
            AppUtils.forUpdateAudit(application);
            applicationMapper.updateByPrimaryKeySelective(application);
        }
    }
}
